<template>
  <el-dialog
    :visible.sync="dialogFormVisible"
    width="60%"
    :append-to-body="true"
    :close-on-click-modal="false"
    center
  >
    <template slot="title">
      <span class="add-main-tital">
        <h4>新增权限</h4>
      </span>
    </template>
    <div class="add-container">
      <div class="add-main">
        <!-- <div class="search-group">
          <div class="search-gropu-item">
            <label>用户名</label>
            <el-input v-model="listQuery.loginname"></el-input>
          </div>
          <div class="search-button">
            <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetlist">重置</el-button>
          </div>
        </div> -->

        <div class="table-list" v-if="authName == 'kf'">
          <el-table
            ref="kftable"
            :data="list"
            :element-loading-text="elementLoadingText"
            height="40vh"
            @selection-change="setSelectRows"
            highlight-current-row
            :header-cell-style="{
              'border-bottom': '1px solid #cbcccd',
              'border-top': '1px solid #cbcccd',
            }"
          >
            <el-table-column
              show-overflow-tooltip
              type="selection"
            ></el-table-column>
            <el-table-column show-overflow-tooltip label="序号" width="95">
              <template #default="scope">
                {{
                  scope.$index +
                  listQuery.pageSize * (listQuery.pageIndex - 1) +
                  1
                }}
              </template>
            </el-table-column>
            <el-table-column
              show-overflow-tooltip
              prop="kufangdm"
              label="库房代码"
            ></el-table-column>
            <el-table-column
              show-overflow-tooltip
              prop="mingcheng"
              label="库房名称"
            ></el-table-column>
            <el-table-column
              show-overflow-tooltip
              prop="kufanglxmc"
              label="库房类型"
            ></el-table-column>
            <el-table-column
              show-overflow-tooltip
              prop="zhiliangzkmc"
              label="质量状况"
            ></el-table-column>
          </el-table>
          <!-- <el-pagination background :current-page="listQuery.pageIndex" :page-size="listQuery.pageSize" :layout="layout"
            :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination> -->
        </div>
        <div class="table-list" v-else>
          <el-table
            ref="zytable"
            :data="list"
            :element-loading-text="elementLoadingText"
            height="40vh"
            @selection-change="setSelectRows"
            highlight-current-row
            :header-cell-style="{
              'border-bottom': '1px solid #cbcccd',
              'border-top': '1px solid #cbcccd',
            }"
          >
            <el-table-column
              show-overflow-tooltip
              type="selection"
            ></el-table-column>
            <el-table-column show-overflow-tooltip label="序号" width="95">
              <template #default="scope">
                {{
                  scope.$index +
                  listQuery.pageSize * (listQuery.pageIndex - 1) +
                  1
                }}
              </template>
            </el-table-column>
            <el-table-column
              show-overflow-tooltip
              prop="id"
              label="分类ID"
            ></el-table-column>
            <el-table-column
              show-overflow-tooltip
              prop="zhuanyemc"
              label="分类名称"
            ></el-table-column>
            <el-table-column
              show-overflow-tooltip
              prop="zhuanyedm"
              label="分类代码"
            ></el-table-column>
          </el-table>
          <!-- <el-pagination background :current-page="listQuery.pageIndex" :page-size="listQuery.pageSize" :layout="layout"
            :total="total" @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination> -->
        </div>
      </div>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button
        size="medium"
        type="primary"
        icon="el-icon-s-check"
        @click="save"
      >
        确定
      </el-button>
      <el-button size="medium" icon="el-icon-delete" @click="close">
        取 消
      </el-button>
    </span>
  </el-dialog>
</template>
<script>
  import {
    queryUserKufang,
    setKufangUser,
    queryKufangList,
    queryKufangshowList,
    setZhuanyeUser,
    queryUserZhuanye,
  } from '@/api/cangkujichu'
  import { getlistWZZY, getlistWZZYAll } from '@/api/biaozhun'
  let _listQuery = {
    pageIndex: 1,
    pageSize: 10,
  }
  export default {
    name: 'Addauth',
    props: {
      authName: {
        type: String,
        default: 'kf',
        require: true,
      },
    },
    data() {
      return {
        listQuery: Object.assign({}, _listQuery),
        show: true,
        layout: 'prev, pager, next',
        total: 0,
        listLoading: true,
        elementLoadingText: '正在加载...',
        selectRows: '',
        ruleForm: {
          rkdh: '',
          wzmc: '',
          wzqf: '',
          rkzt: '',
          rklx: '',
          datalist: [],
        },
        // rules: {
        //   rkdh: [{ required: true, message: '请输入入库单号', trigger: 'blur' }],
        // },
        rules: {
          sl: [{ required: true, message: '请输入数量', trigger: 'blur' }],
          ffkf: [{ required: true, message: '请选择库房', trigger: 'blur' }],
        },
        tabheight: 500,
        importShow: false,
        kfList: [
          { value: '库房1', label: '库房1' },
          { value: 2, label: '库房2' },
          { value: 3, label: '库房3' },
        ],
        dialogFormVisible: false,
        list: [],
        yhkufanglist: [],
        yhzhuanyelist: [],
        selectrow: {},
      }
    },
    created() {},
    mounted() {},
    methods: {
      setSelectRows(val) {
        this.selectRows = val
      },
      formatData(row, column, cellValue, index) {
        var s = new Date(cellValue).toLocaleString()
        return s
      },
      async getData() {
        if (this.authName == 'kf') {
          const { data } = await queryKufangshowList({})
          this.list = data
          // this.total = data.total
          this.$nextTick(() => {
            this.list.forEach((item) => {
              this.yhkufanglist.forEach((it) => {
                if (item.id == it.kufangid) {
                  this.$refs.kftable.toggleRowSelection(item, true)
                }
              })
            })
          })
        } else {
          console.log('分类')
          const { data } = await getlistWZZYAll({})
          this.list = data
          this.$nextTick(() => {
            this.list.forEach((item) => {
              this.yhzhuanyelist.forEach((it) => {
                if (item.id == it.zhuanyeid) {
                  this.$refs.zytable.toggleRowSelection(item, true)
                }
              })
            })
          })
        }
      },
      async showEdit(row) {
        this.dialogFormVisible = true
        this.selectrow = row
        if (this.authName == 'kf') {
          let { data } = await queryUserKufang({})
          this.yhkufanglist = data
        } else {
          let { data } = await queryUserZhuanye({ yonghuid: this.selectrow.id })
          this.yhzhuanyelist = data
        }
        this.getData()
      },
      close() {
        //  this.$refs['form'].resetFields()
        //  this.form = this.$options.data().form
        this.dialogFormVisible = false
        this.resetlist()
      },
      async save() {
        if (this.selectRows.length == 0) {
          this.$message({
            message: '请先勾选数据',
            type: 'warning',
          })
          return
        }
        let selectkfIds = this.selectRows.map((item) => item.id).join(',')
        let data = {
          ids: selectkfIds,
          userid: this.selectrow.id,
        }
        if (this.authName == 'kf') {
          const { msg } = await setKufangUser(data)
          this.$message({
            message: msg,
            type: 'success',
          })
        } else {
          const { msg } = await setZhuanyeUser(data)
          this.$message({
            message: msg,
            type: 'success',
          })
        }
        this.close()
      },
      goBack() {
        this.$parent.addshow = false
      },

      selectChange(e) {},
      search() {
        this.getData()
      },
      resetlist() {
        this.listQuery = Object.assign({}, _listQuery)
        this.getData()
      },
      handleSizeChange(val) {
        this.listQuery.pageSize = val
        this.getData()
      },
      handleCurrentChange(val) {
        this.listQuery.pageIndex = val
        this.getData()
      },
    },
  }
</script>
<style lang="scss" scoped>
  .search-group {
    display: flex;
    flex-flow: row wrap;

    .search-gropu-item {
      padding: 10px 20px;
      width: calc((100% - 20px * 8) / 2);
      display: flex;

      label {
        width: 0.8rem;
        white-space: nowrap;
        line-height: 0.32rem;
        margin-right: 0.1rem;
      }

      .el-select {
        width: 100%;
      }
    }

    .search-gropu-item-time {
      width: 500px !important;
      padding: 10px 20px;
      display: flex;

      label {
        white-space: nowrap;
        line-height: 32px;
        margin-right: 10px;
      }
    }

    .search-button {
      margin-left: auto;
      padding: 0.1rem 0.2rem;

      .el-button {
        font-size: 0.14rem !important;
      }
    }
  }

  .add-container {
    width: 100%;
    height: 100%;
    z-index: 1000;
    background-color: #f6f8f9;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    overflow: hidden;

    .add-top {
      ::v-deep .el-page-header__title {
        font-size: 16px !important;
      }
    }

    .add-main {
      flex: 1;
      font-weight: bold;
      display: flex;
      flex-direction: column;
      overflow: auto;

      .add-main-top {
        padding: 0.2rem;
        background: #fff;
        border-radius: 0.05rem;

        .add-main-tital {
          font-size: 0.18rem;
          border-left: 0.1rem solid rgb(121, 189, 255);
          padding-left: 0.1rem;

          p {
            margin: 0;
          }
        }

        .search-gropu-item {
          margin-top: 0.2rem;
          width: 90%;
          display: flex;
          // label {
          //   white-space: nowrap;
          //   line-height: 32px;
          //   margin-right: 10px;
          // }
        }

        .el-select {
          width: 100%;
        }
      }

      .add-main-list {
        flex: 1;
        background: #fff;
        box-sizing: border-box;

        .list-button-group {
          display: flex;
          flex-direction: row;
          padding: 15px;

          .button-item {
            padding: 0px 10px;
          }

          .button-item:first-child,
          &:last-child {
            padding: 0px 10px 0 0;
          }
        }
      }

      .el-button {
        font-size: 0.14rem !important;
      }

      ::v-deep {
        .elx-table--header-wrapper {
          background-color: rgb(233, 243, 255);
        }

        .elx-table {
          font-size: 0.14rem !important;
        }

        .elx-body--column {
          height: 0.48rem !important;
          line-height: 0.24rem;
        }

        .elx-header--column {
          background-image: none !important;
          height: 0.48rem !important;
        }

        .el-date-editor.el-input,
        .el-date-editor.el-input--inner {
          width: 100% !important;
        }

        .el-form-item--small.el-form-item {
          margin-bottom: 0px;
          margin-top: 18px;
        }
      }

      .submit-button {
        margin-top: 20px;
        display: flex;
        justify-content: center;
      }
    }
  }
</style>
